import 'package:flutter/material.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/http/api_shop.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/express_company_model.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';

/// @description:
/// @author tangzhentao
/// @created at 19:33 2025/3/8


class SetExpressViewmodel extends Viewmodel {
  /// 订单id
  final int orderId;
  /// 是否店铺订单
  final bool isShopOrder;

  /// 快递单号输入框相关
  final TextEditingController controller = TextEditingController();
  final FocusNode expressNuNode = FocusNode();

  bool _calClick = false;
  /// 是否可点击
  bool get canClick => _calClick;

  Company? _company;
  /// 选择的快递公司
  Company? get company => _company;

  SetExpressViewmodel(this.orderId, this.isShopOrder);

  /// 输入框已变化
  void onTextChanged(String value) {
    final click = value.isNotEmpty;
    if (_calClick != click) {
      _calClick =click;
      notifyListeners();
    }
  }

  /// 填快递单号
  Future setExpress() async {
    if (isShopOrder) {
      final result = await Http().request<dynamic>(ApiShop.dispatch, successText: '已发货', data: {
        'id': orderId, 'nu': controller.text, 'com': _company?.simpleName?? 'auto'
      });
      if (result.success) {
        XfgNav.pop(true);
      }
    } else {
      final result = await Http().request<dynamic>(ApiGold.recycleSetExpress, successText: '填写成功~', data: {
        'id': orderId, 'express_nu': controller.text, 'express_com': _company?.simpleName?? 'auto'
      });
      if (result.success) {
        XfgNav.pop(true);
      }
    }
  }

  /// 选择快递公司
  Future pickupCompany() async {
    final result = await XfgNav.pushNamed(Routes.settingsSetExpressCompany);
    if (result != null && result is Company) {
      _company = result;
      notifyListeners();
    }
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}